
require "MCP23017"   

--***********  MCP23017的IO端口配置 *********************--

--chip1,输出: 继电器和电机驱动
function TurnOn_Relay1()    MCP23017.Chip1.output[1] = bit.set( MCP23017.Chip1.output[1], 2)  end
function TurnOff_Relay1()   MCP23017.Chip1.output[1] = bit.clear( MCP23017.Chip1.output[1], 2)  end

function TurnOn_Relay2()    MCP23017.Chip1.output[1] = bit.set( MCP23017.Chip1.output[1], 3)  end
function TurnOff_Relay2()   MCP23017.Chip1.output[1] = bit.clear( MCP23017.Chip1.output[1], 3)  end

function TurnOn_CCW()        MCP23017.Chip1.output[1] = bit.set( MCP23017.Chip1.output[1], 1)  end
function TurnOff_CCW()       MCP23017.Chip1.output[1] = bit.clear( MCP23017.Chip1.output[1], 1)  end

function TurnOn_CW()        MCP23017.Chip1.output[1] = bit.set( MCP23017.Chip1.output[1], 0)  end
function TurnOff_CW()       MCP23017.Chip1.output[1] = bit.clear( MCP23017.Chip1.output[1], 0)  end

--输入
function LimitSwitchCW_On()       return bit.isclear(MCP23017.Chip1.input[1], 6)   end
function LimitSwitchCCW_On()       return bit.isclear(MCP23017.Chip1.input[1], 7)   end
function Wind_On()       return bit.isset(MCP23017.Chip1.input[2], 0)   end

function Keypads()      return bit.band( bit.rshift( MCP23017.Chip1.input[2], 4), 0x0f) end     -- 返回手柄值
function Key_Aim()      return bit.isclear(MCP23017.Chip1.input[2], 7)   end
function Key_CW()       return bit.isclear(MCP23017.Chip1.input[2], 6)   end
function Key_CCW()      return bit.isclear(MCP23017.Chip1.input[2], 5)   end
function Key_Save()     return bit.isclear(MCP23017.Chip1.input[2], 4)   end



--chip2: LED,蜂鸣器,拨码开关等
function TurnOn_Red_LED()   MCP23017.Chip2.output[1] = bit.clear( MCP23017.Chip2.output[1], 7)  end
function TurnOff_Red_LED()  MCP23017.Chip2.output[1] = bit.set( MCP23017.Chip2.output[1], 7)  end

function TurnOn_Green_LED()   MCP23017.Chip2.output[1] = bit.clear( MCP23017.Chip2.output[1], 6)  end
function TurnOff_Green_LED()  MCP23017.Chip2.output[1] = bit.set( MCP23017.Chip2.output[1], 6)   end

function TurnOn_Yellow_LED()   MCP23017.Chip2.output[1] = bit.clear( MCP23017.Chip2.output[1], 5)  end
function TurnOff_Yellow_LED()  MCP23017.Chip2.output[1] = bit.set( MCP23017.Chip2.output[1], 5)  end

function TurnOn_Blue_LED()   MCP23017.Chip2.output[1] = bit.clear( MCP23017.Chip2.output[1], 4)  end
function TurnOff_Blue_LED()  MCP23017.Chip2.output[1] = bit.set( MCP23017.Chip2.output[1], 4)   end

function TurnOn_Beep()   MCP23017.Chip2.output[1] = bit.set( MCP23017.Chip2.output[1], 0)  end
function TurnOff_Beep()   MCP23017.Chip2.output[1] = bit.clear( MCP23017.Chip2.output[1], 0)  end

-- 拨码开关等，在定时函数内刷新，并将开关值保存在Switch[4]
function Set_Kout(n)    MCP23017.Chip2.output[2] = bit.lshift(n,4)  end      -- n对应Kout3..Kout0; 因Kout在高4bit，直接赋值对低4bit输入没有影响。
function Get_Kin()      return bit.band( MCP23017.Chip2.input[2], 0x0f) end  -- 返回Kin0..3


